﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TPYVA06 
    {
        #region ==dwProd===========
        public static readonly DependencyProperty dwProdProperty = DependencyProperty.Register("dwProd",
            typeof(DataRow), typeof(TPYVA06),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwProdChanged));
        public DataRow dwProd
        {
            get
            {
                return (DataRow)GetValue(dwProdProperty);
            }
            set
            {
                SetValue(dwProdProperty, value);
            }
        }
        private static void OndwProdChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPYVA06;
            if (src != null)
            {
                src.dwProd = (DataRow)e.NewValue; 
            }
        }
        #endregion  
       
        #region ==PRART===========
        public static readonly DependencyProperty PRARTProperty = DependencyProperty.Register("PRART",
            typeof(string), typeof(TPYVA06),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPRARTChanged));
        public string PRART
        {
            get
            {
                return (string)GetValue(PRARTProperty);
            }
            set
            {
                SetValue(PRARTProperty, value);
            }
        }
        private static void OnPRARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TPYVA06;
            if (src != null)
            {
                src.PRART = (string)e.NewValue; 
            }
        }
        #endregion  
        public TPYVA06()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVAART();
            DataContext = MV; 
            InitializeComponent(); 
        }  
         
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            DataSet SResult;
            DataRow dwData;

            dwData = TBMain.NewRow();
            SResult = ((VVAART)MV).QueryVAART(dwData, -1);
            MergeData(SResult);
        }

        public override void NewCommand(object sender, System.Windows.RoutedEventArgs e)
        { 
            dwData = TBMain.Rows.Add(); 
            vwMaster.View.MoveCurrentToLast();
        }

        public override void EditCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            base.EditCommand(sender, e);
        }

        public override void ForwardCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            vwMaster.View.MoveCurrentToNext();
        }

        public override void DeleteCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VVAART)MV).DeleteVAART(dwMaster, 1);

            vwMaster.View.MoveCurrentTo(-1);
            string filter = "VAAKOLL='" + dwMaster["VAAKOLL"].ToString() + "' AND VAAART='" + dwMaster["VAAART"].ToString() + "'";
            DataRow[] rows = TBMain.Select(filter);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
        }

        public override void CopyCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            DataRow dwData;
            dwData = TBMain.Rows.Add();
            dwData.ItemArray = dwMaster.ItemArray;
            vwMaster.View.MoveCurrentToLast();
        }
        public DataRow dwExternal;

        public override void Save()
        {
            base.Save();
            if (dwExternal == null)
                return;
            ((VVAART)MV).SaveVAART(dwExternal, app.company);
            switch (dwExternal["VAAARTST"].ToString())
            {
                case "G":
                    break; 
            }
            dwExternal = null;
        }
        public override void SaveCommand(object sender, System.Windows.RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            try
            {
                dwExternal = dwMaster;
                Save();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

     
        private void VAAART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
             if (e.Key != System.Windows.Input.Key.Enter)
                    return;
             dwMaster["VAADART"]=Helper.Left( dwMaster["VAAART"].ToString().PadRight(20),20);
             dwMaster["VAADVAR"] = "  ";
             dwMaster["VAAPART"] = dwMaster["VAADART"];
        }
    }
}
